//
//  OrderTool.m
//  DanKU
//
//  Created by EDY on 2023/11/12.
//

#import "OrderTool.h"

@implementation OrderTool


+ (NSString *)stringFromTimestampString:(NSString *)timestampString {
    // 将字符串类型的时间戳转换为NSTimeInterval（以秒为单位）
    NSTimeInterval timestampSeconds = [timestampString doubleValue] / 1000.0;
    
    // 创建一个NSDate对象，使用时间戳初始化
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestampSeconds];
    
    // 创建一个NSDateFormatter对象，用于格式化日期
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    // 设置日期格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    // 使用NSDateFormatter将NSDate对象转换为字符串
    NSString *formattedDate = [dateFormatter stringFromDate:date];
    
    return formattedDate;
}


+ (NSString *)convertDate:(NSString *)inputDateString
       fromFormat:(NSString *)inputFormat
         toFormat:(NSString *)outputFormat {
    
    // 创建输入日期格式化器
    NSDateFormatter *inputDateFormatter = [[NSDateFormatter alloc] init];
    [inputDateFormatter setDateFormat:inputFormat];
    
    // 将输入字符串转换为 NSDate 对象
    NSDate *inputDate = [inputDateFormatter dateFromString:inputDateString];
    
    // 创建输出日期格式化器
    NSDateFormatter *outputDateFormatter = [[NSDateFormatter alloc] init];
    [outputDateFormatter setDateFormat:outputFormat];
    
    // 将 NSDate 对象转换为输出格式的字符串
    NSString *outputDateString = [outputDateFormatter stringFromDate:inputDate];
    
    return outputDateString;
}


+ (NSString *)formatAmountForIndia:(NSString *)amountString {
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    numberFormatter.numberStyle = NSNumberFormatterDecimalStyle;
    numberFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_IN"];

    NSDecimalNumber *amountDecimal = [NSDecimalNumber decimalNumberWithString:amountString];
    if ([amountDecimal compare:[amountDecimal decimalNumberByRoundingAccordingToBehavior:[NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundDown scale:0 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO]]] == NSOrderedSame) {
        numberFormatter.maximumFractionDigits = 0;
        numberFormatter.minimumFractionDigits = 0;
    } else {
        numberFormatter.maximumFractionDigits = 2;
        numberFormatter.minimumFractionDigits = 2;
    }

    NSString *formattedAmount = [numberFormatter stringFromNumber:amountDecimal];
    return [NSString stringWithFormat:@"₹%@", formattedAmount];
}


+ (NSDictionary *)parseHTMLString:(NSString *)htmlString {
    NSMutableDictionary *result = [NSMutableDictionary dictionary];

    // Extract color value
    NSError *error = nil;
    NSRegularExpression *colorRegex = [NSRegularExpression regularExpressionWithPattern:@"color=['\"]#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})['\"]"
                                                                                 options:NSRegularExpressionCaseInsensitive
                                                                                   error:&error];
    NSTextCheckingResult *colorMatch = [colorRegex firstMatchInString:htmlString
                                                              options:0
                                                                range:NSMakeRange(0, [htmlString length])];
    NSString *colorValue = nil;
    if (colorMatch) {
        NSRange colorRange = [colorMatch rangeAtIndex:1];
        colorValue = [htmlString substringWithRange:colorRange];
    }

    // Extract font value
    NSRegularExpression *fontRegex = [NSRegularExpression regularExpressionWithPattern:@"<font[^>]*>(.*?)</font>"
                                                                                options:NSRegularExpressionCaseInsensitive
                                                                                  error:&error];
    NSTextCheckingResult *fontMatch = [fontRegex firstMatchInString:htmlString
                                                            options:0
                                                              range:NSMakeRange(0, [htmlString length])];
    NSString *fontValue = nil;
    if (fontMatch) {
        NSRange fontRange = [fontMatch rangeAtIndex:1];
        fontValue = [htmlString substringWithRange:fontRange];
    }

    // Add values to the result dictionary
    if (colorValue) {
        result[@"Color"] = [NSString stringWithFormat:@"#%@", colorValue];
    } else {
        result[@"Color"] = @"Not Found";
    }

    if (fontValue) {
        result[@"Font"] = fontValue;
    } else {
        result[@"Font"] = @"Not Found";
    }

    return [result copy];
}


@end
